const { fnm, fnm_s } = require('../test-utils');
const processModule = require('./01module');
const processChunk = require('./02chunk');
const processRecord = require('./03record');
const processAssets = require('./04assets');

class TestCompilationPlugin {
  /**
   *
   * @param {('Module'|'Chunk'|'Record'|'Assets'|'Other')[]} types
   */
  constructor(types = ['Other']) {
    /**
     * @type {typeof types}
     */
    this.types = types;
  }

  /**
   *
   * @param {import('webpack').Compiler} compiler
   */
  apply(compiler) {
    const name = '个人测试 TestCompilationPlugin';

    compiler.hooks.compilation.tap(name, (compilation, compilationParams) => {
      if (this.types.includes('Module')) {
        processModule(compilation);
      }
      if (this.types.includes('Chunk')) {
        processChunk(compilation);
      }
      if (this.types.includes('Record')) {
        processRecord(compilation);
      }
      if (this.types.includes('Assets')) {
        processAssets(compilation);
      }
      if (this.types.includes('Other')) {
        compilation.hooks.seal.tap(name, () => {
          console.log('------------- seal');
        });
        compilation.hooks.unseal.tap(name, () => {
          console.log('------------- unseal');
        });
        // OptimizeToChunks.prototype.apply(compilation);
        // Record(name, compilation);

        compilation.hooks.additionalAssets.tapAsync(name, (callback) => {
          console.log('------------- additionalAssets');
          callback();
        });

        compilation.hooks.needAdditionalSeal.tap(name, () => {
          console.log('------------- needAdditionalSeal');
        });
        compilation.hooks.afterSeal.tapAsync(name, (callback) => {
          console.log('------------- afterSeal');
          callback();
        });

        compilation.hooks.needAdditionalPass.tap(name, () => {
          console.log('------------- needAdditionalPass');
        });
        compilation.hooks.childCompiler.tap(name, (childCompiler, compilerName, compilerIndex) => {
          console.log('------------- childCompiler');
        });
      }
    });
  }
}

exports.TestCompilationPlugin = TestCompilationPlugin;

// // @deprecated
// compilation.hooks.normalModuleLoader.tap(name, () => {
//   console.log("------------- normalModuleLoader");
//   // DEP_WEBPACK_COMPILATION_NORMAL_MODULE_LOADER_HOOK
//   //  moved to NormalModule.getCompilationHooks(compilation).loader
// });
